package com.hu;

import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Stream;

/**
 * @author 00355760
 * @功能：
 * @版本： V1
 * @版权： 信息技术有限公司（http://www.mychery.com/）
 * @作者： 磐石战队开发组  00355760
 * @日期： 2024/9/14 14:14
 */
public class Stream01 {

    public static void main(String[] args) {

        List<Author> authors = getAuthors();
        HashMap<String, Integer> map = new HashMap<>();
        map.put("a", 17);
        map.put("b", 19);
        //单列集合
        Stream<Author> stream = authors.stream();
        //双列集合,先转换为单列集合
        Stream<Map.Entry<String, Integer>> stream1 = map.entrySet().stream();
        stream1.filter(stringIntegerEntry -> stringIntegerEntry.getValue()>16).forEach(stringIntegerEntry -> System.out.println(stringIntegerEntry.getKey()+":"+stringIntegerEntry.getValue()));
        test03();
        test04();
    }

    private static void test03() {

        Integer[] arr = {1,2,3,4,5};
        Stream<Integer> stream = Arrays.stream(arr);
        stream.distinct().filter(value -> value>2)
        .forEach(System.out::println);
    }

    //并行流 .parallel()进行转换或者通过.parallelStream()直接创建并行流
    private static void test04() {

        Integer[] arr = {1,2,3,4,5,6,7,8,9,10};
        Stream<Integer> stream = Arrays.stream(arr);
        stream.parallel().peek(integer -> System.out.println(integer+"--"+Thread.currentThread().getName()))
        .filter(value -> value>2).reduce(Integer::sum).ifPresent(System.out::println);
    }


    public static List<Author> getAuthors() {

        Author author = new Author(1L, "蒙多", 33, "一个从菜刀中明悟哲理的祖安人", null);
        Author author2 = new Author(2L, "亚拉索", 15, "狂风也追逐不上，林间有风", null);
        Author author3 = new Author(3L, "易", 14, "有生之uries", null);
        Author author4 = new Author(3L, "易", 14, "有生之uries", null);

        List<Book> books1 = new ArrayList<>();
        List<Book> books2 = new ArrayList<>();
        List<Book> books3 = new ArrayList<>();
        books1.add(new Book(1L, "刀的两侧是光明与黑暗", "哲学,爱情", 88,"用一把刀划分了爱情"));
        books1.add(new Book(2L, "一个人不能死在同一把刀下", "个人成长,爱情", 99,"讲述如何从失败到成功"));
        books2.add(new Book(3L, "那风吹不到的地方", "哲学", 85,"带你用思维去领略世界的尽头"));
        books2.add(new Book(3L, "那风吹不到的地方", "哲学", 85,"带你用思维去领略世界的尽头"));
        books2.add(new Book(4L, "吹或不吹", "爱情,个人传记", 56,"纵使困顿难行，亦当砥砺奋进"));
        books3.add(new Book(5L, "你的🗡就是我的🗡", "哲学", 56,"买桂花同载酒"));
        books3.add(new Book(6L, "风吹半夏", "个人传记", 100,"世子之争素来如此"));
        books3.add(new Book(6L, "风吹半夏", "个人传记", 100,"世子之争素来如此"));

        author.setBooks(books1);
        author2.setBooks(books2);
        author3.setBooks(books3);
        author4.setBooks(books3);
        List<Author> authorList = new ArrayList<>(Arrays.asList(author, author2, author3, author4));

        return authorList;
    }

    public static Author getAuthors2() {
        Author author = new Author(1L, "蒙多", 33, "一个从菜刀中明悟哲理的祖安人", null);
        return author;
    }
}
